package com.module.app.widget.share.type;

/**
 * @author bsnl_yanxuwen
 * @date 2020/8/24 17:46
 * Description :
 */

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.module.app.widget.share.bean.ShareDefaultBean;
import com.module.base.utils.DisplayUtils;
import com.module.base.utils.QRCodeUtils;
import com.module.base.utils.ToastUtils;
import com.bisinuolan.app.base.bsnl_share.ShareBuilder;
import com.bisinuolan.app.base.bsnl_share.annotation.ShareType;
import com.bisinuolan.app.base.bsnl_share.bean.ShareImgsBean;
import com.bisinuolan.app.base.bsnl_share.listener.layout.BaseDefaultLayoutType;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.module.app.R;
import com.module.frame.glide.GlideApp;

import java.util.ArrayList;
import java.util.List;

/**
 * @author bsnl_yanxuwen
 * @date 2020/8/20 16:24
 * Description :
 * 默认的海报图片
 * <p>
 * WechatSDKv 集成分享，暂时没接入，使用系统自带
 */
public class DefaultLayoutType extends BaseDefaultLayoutType<ShareDefaultBean> {
    //    public WechatSDK wechatSDK;
    public ShareBuilder shareBuilder;

    /**
     * 使用自己的预览图
     */
    @Override
    public boolean isOpenPreview() {
        return false;
    }

    @Override
    public int geLoadLayoutId() {
        return R.layout.app_dialog_loading;
    }

    @Override
    public int getLayoutId() {
        return R.layout.app_item_default_poster;
    }

    @Override
    public ShareBuilder getShareBuilder() {
        if (shareBuilder == null) {
            shareBuilder = new ShareBuilder();
            shareBuilder
                    .addButton(ShareType.TYPE_WX, "微信好友", R.mipmap.app_btn_wechat2)
                    .addButton(ShareType.TYPE_FRIENDS, "朋友圈", R.mipmap.app_btn_friends2)
                    .addButton(ShareType.TYPE_SAVE, "保存图片", R.mipmap.app_btn_down);
        }

        return shareBuilder;
    }


    @Override
    public void init() {
//        wechatSDK = new WechatSDK(context, ISDK.SDK.WECHAT);
    }

    @Override
    public void initView(View view, int position) {
        ShareDefaultBean bean = getList().get(position);
        TextView tvTitle = view.findViewById(R.id.tv_title);
        TextView tvName = view.findViewById(R.id.tv_name);
        if (bean.isOriginal) {
            //原图
            view.findViewById(R.id.layout_top).setVisibility(View.GONE);
            view.findViewById(R.id.iv_original).setVisibility(View.VISIBLE);
            return;
        }

        tvName.setText(bean.name);
        tvTitle.setText(bean.title);
    }

    @Override
    public void onClick(int shareType, String text) {
        boolean isComplete = true;
        try {
            if (getCurT().isMultiShare) {
                for (ShareImgsBean shareImgsBean : listShareImgs) {
                    if (shareImgsBean.getBitmap() == null && shareType != ShareType.TYPE_CANCEL) {
                        Toast.makeText(context, "图片还没加载完成，请稍后...", Toast.LENGTH_SHORT).show();
                        setTemporaryCancelAutoDismiss(true);
                        isComplete = false;
                        break;
                    }
                }
            } else {
                if (listShareImgs.get(getCurPosition()).getBitmap() == null && shareType != ShareType.TYPE_CANCEL) {
                    Toast.makeText(context, "图片还没加载完成，请稍后...", Toast.LENGTH_SHORT).show();
                    setTemporaryCancelAutoDismiss(true);
                    isComplete = false;
                }
            }

        } catch (Exception e) {
        }
        if (!isComplete) {
            return;
        }
        switch (shareType) {
            case ShareType.TYPE_WX:
            case ShareType.TYPE_FRIENDS:
                if (getCurT().isMultiShare && listShareImgs.size() > 1) {
                    share(shareType, false);
                } else {
                    //这边先使用系统自带的分享
                    shareCurrent(shareType, false);
//                    if (wechatSDK != null && wechatSDK.isWechatExist()) {
//                        ShareImgsBean shareImgsBean = listShareImgs.get(this.getCurPosition());
//                        wechatSDK.shareImg(ShareType.TYPE_WX == shareType ? 0 : 1, shareImgsBean.getBitmap());
//                    }
                }

                break;
            case ShareType.TYPE_SAVE:
                List imgs = new ArrayList();
                boolean isMultiShare = getCurT().isMultiShare;
                if (listShareImgs == null) {
                    return;
                }
                for (ShareImgsBean bean : listShareImgs) {
                    imgs.add(bean.getBitmap());
                    if (!isMultiShare) {
                        break;
                    }
                }
                if (getCurT().isMultiShare && listShareImgs.size() > 1) {
                    share(shareType, true);
                } else {
                    //这边先使用系统自带的分享
                    shareCurrent(shareType, true);
                }
                ToastUtils.showShort("图片保存成功");

                break;
            case ShareType.TYPE_POSTER:
                List list = new ArrayList();
                if (listShareImgs == null) {
                    return;
                }
                for (ShareImgsBean shareImgsBean : listShareImgs) {
                    list.add(shareImgsBean.getBitmap());
                }
//                // 多图预览
//                new XPopup.Builder(context)
//                        .asImageViewer(null, getCurPosition(), list, null, new XPopupGlideLoader(R.drawable.default_logo))
//                        .show();
                break;

        }
    }

    /**
     * 加载图片,该方法是在线程执行，所有不能glide直接加载图片
     */
    @Override
    public void loadImage(View parent, int position) {
        ShareDefaultBean bean = getList().get(position);
        try {
            if (bean.isOriginal) {
                //原图
                final ImageView iv_original = parent.findViewById(R.id.iv_original);
                Bitmap bitmapPic = null;
                if (!TextUtils.isEmpty(bean.pic)) {
                    bitmapPic = Glide.with(context).asBitmap().load(bean.pic).submit().get();
                } else if (bean.picResId != 0) {
                    bitmapPic = Glide.with(context).asBitmap().load(bean.picResId).submit().get();
                }
                Bitmap finalBitmapPic = bitmapPic;
                ((Activity) context).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (finalBitmapPic != null) {
                            iv_original.setImageBitmap(finalBitmapPic);
                        }
                    }
                });
                return;
            }
            final ImageView iv_img = parent.findViewById(R.id.iv_img);
            final ImageView iv_avatar = parent.findViewById(R.id.iv_header);
            final ImageView iv_qr = parent.findViewById(R.id.iv_qr);
            final Bitmap bitmapPic;
            if (bean.picResId != 0) {
                bitmapPic = Glide.with(context).asBitmap().load(bean.picResId).submit().get();
            } else {
                bitmapPic = Glide.with(context).asBitmap().load(bean.pic).submit().get();
            }
            final Bitmap bitmapMini;
            if (!TextUtils.isEmpty(bean.mini_url_net)) {
                bitmapMini = Glide.with(context).asBitmap().load(bean.mini_url_net).submit().get();
            } else {
                bitmapMini = QRCodeUtils.get2(bean.mini_url, 500, 500);
            }
            final Bitmap bitmapHead = GlideApp.with(context)
                    .asBitmap()
                    .load(R.mipmap.app_icon_logo)
                    .transform(new CircleCrop()).submit().get();
            ((Activity) context).runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    iv_img.setImageBitmap(bitmapPic);
                    iv_avatar.setImageBitmap(bitmapHead);
                    iv_qr.setImageBitmap(bitmapMini);
                    if (bean.scaleType == ImageView.ScaleType.MATRIX) {
                        setMatrix(iv_img, bitmapPic.getWidth(), bitmapPic.getHeight(), DisplayUtils.dip2px(360), DisplayUtils.dip2px(360));
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void setMatrix(ImageView imageView, float drawableWidth, float drawableHeight, int viewWidth, int viewHeight) {
        imageView.setScaleType(ImageView.ScaleType.MATRIX);
        Matrix matrix = new Matrix();
        float scale = viewWidth / drawableWidth;
        matrix.setScale(scale, scale);
        imageView.setImageMatrix(matrix);
    }

}
